<?php
/**
 *
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/12/19
 * @copyright PanxSoft Inc.
 */
namespace App\Models\Concerns;


use App\Services\Utils\Memorable;
use Illuminate\Database\Eloquent\Model;

/**
 * 缓存查询
 */
trait ModelMemorable
{
    /**
     * @param $key
     * @param array $columns
     *
     * @return mixed
     */
    public static function memoFind($key, $columns = ['*'])
    {
        return Memorable::singleton(function (int $key, $columns) {
            /** @var Model $model */
            $model = (new static);
            return $model->find($key, $columns);
        })($key, $columns);
    }

    /**
     * @param array $where
     * @param array $columns
     *
     * @return mixed
     */
    public static function memoWhereFirst(array $where, $columns = ['*'])
    {
        return Memorable::singleton(function ($where, $columns) {
            /** @var Model $model */
            $model = (new static);
            return $model->where($where)->first($columns);
        })($where, $columns);
    }

    /**
     * @param array $where
     * @param array $columns
     *
     * @return mixed
     */
    public static function memoWhereGet(array $where, $columns = ['*'])
    {
        return Memorable::singleton(function ($where, $columns) {
            /** @var Model $model */
            $model = (new static);
            return $model->where($where)->get($columns);
        })($where, $columns);
    }
}
